package com.xiaoshuidi.cloud.module.finance.dal.mysql.merchant;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoshuidi.cloud.module.finance.controller.admin.merchant.vo.merchant.PayMerchantCreateReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.merchant.vo.merchant.PayMerchantExportReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.merchant.vo.merchant.PayMerchantPageReqVO;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.QueryWrapperX;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.merchant.PayMerchantDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface PayMerchantMapper extends BaseMapperX<PayMerchantDO> {

    default PageResult<PayMerchantDO> selectPage(PayMerchantPageReqVO reqVO) {
        return selectPage(reqVO, new QueryWrapperX<PayMerchantDO>()
                .likeIfPresent("no", reqVO.getNo())
                .likeIfPresent("name", reqVO.getName())
                .likeIfPresent("short_name", reqVO.getShortName())
                .eqIfPresent("status", reqVO.getStatus())
                .eqIfPresent("remark", reqVO.getRemark())
                .betweenIfPresent("create_time", reqVO.getCreateTime())
                .orderByDesc("id"));
    }

    default List<PayMerchantDO> selectList(PayMerchantExportReqVO reqVO) {
        return selectList(new QueryWrapperX<PayMerchantDO>()
                .likeIfPresent("no", reqVO.getNo())
                .likeIfPresent("name", reqVO.getName())
                .likeIfPresent("short_name", reqVO.getShortName())
                .eqIfPresent("status", reqVO.getStatus())
                .eqIfPresent("remark", reqVO.getRemark())
                .betweenIfPresent("create_time", reqVO.getCreateTime())
                .orderByDesc("id"));
    }

    /**
     * 根据商户名称模糊查询商户集合
     *
     * @param merchantName 商户名称
     * @return 商户集合
     */
    default List<PayMerchantDO> getMerchantListByName(String merchantName) {
        return this.selectList(new QueryWrapperX<PayMerchantDO>()
                .likeIfPresent("name", merchantName));
    }

    default int updateByThirdNo(String userId, PayMerchantDO payMerchantDO) {
        return update(payMerchantDO, new LambdaQueryWrapper<PayMerchantDO>()
                .eq(PayMerchantDO::getThirdId, userId));
    }

    default PayMerchantDO getMerchantByAppartmentAndNo(PayMerchantCreateReqVO req) {
        return this.selectOne( new LambdaQueryWrapper<PayMerchantDO>()
                .eq(PayMerchantDO::getNo, req.getNo())
                .eq(PayMerchantDO::getApartmentId, req.getApartmentId())
                .eq(PayMerchantDO::getName, req.getName()));
    }
}
